package com.attendance.sz.utils;

import android.app.Activity;
import android.app.Application;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;
import android.view.WindowManager;

import com.attendance.sz.Splash;
import com.bumptech.glide.Glide;
import com.bumptech.glide.MemoryCategory;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
import com.tencent.bugly.crashreport.CrashReport;
import com.yc.nfc.Convert;

import org.xutils.x;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class MyApplication extends Application {
    private static final String LOGTAG = "martrin";
    private static MyApplication myApplication;
//    private RefWatcher refWatcher;

    public static ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();

    private boolean gVisibleLogWin = false;//true--显示日志设置对话框窗口,否则不
    private boolean gDEBUGNET = false;//true;// 通过网络输出
    private String gstrDestIP = "192.168.0.71";
    private int giDestPort = 8080;
    String poto;
    private byte[] cardNum;
    private byte[] cardOne;
    private String androidid = "";// android 取唯一编号
    private String b = "hello";// 全局变量
    // //false;//true;//模拟USB的出厂卡和生产卡,true为模拟，否则为真机
    private boolean bSimulater = true;// 全局的，true模拟器, false真机

    public String getB() {
        return this.b;
    }

    public void setB(String c) {
        this.b = c;
    }

    public boolean getSimulater() {
        return this.bSimulater;
    }

    public void setSimulater(boolean c) {
        this.bSimulater = c;
        // add by martrin 假如有需要可以真机也可模模快试试试看:)
        // this.bSimulater = true;//true;//false;
    }

    public String getAndroidid() {
        return this.androidid;
    }

    public void setAndroidid(String c) {
        this.androidid = c;
    }


    public boolean getgVisibleLogWin() {
        return this.gVisibleLogWin;
    }

    public void setgVisibleLogWin(boolean c) {
        this.gVisibleLogWin = c;

    }


    public boolean getDEBUGNET() {
        return this.gDEBUGNET;
    }

    public void setDEBUGNET(boolean c) {
        this.gDEBUGNET = c;

    }

    public String getgstrDestIP() {
        return this.gstrDestIP;
    }

    public void setgstrDestIP(String c) {
        this.gstrDestIP = c;
    }
    public String getCardNum() {
        String hex = Convert.ByteArrToHex(cardNum, null);
        BigInteger id = new BigInteger(hex, 16);
        return String.valueOf(id.longValue());

    }



    public void setPoto(String poto) {
        this.poto = poto;
    }
    public String getPoto(){
        return this.poto;
    }
    public String getCardOne() {
        String hex = Convert.ByteArrToHex(cardOne, null);
        BigInteger id = new BigInteger(hex, 16);
        return String.valueOf(id.longValue());

    }
    public int getgiDestPort() {
        return this.giDestPort;
    }

    public void setgiDestPort(int c) {
        this.giDestPort = c;
    }


    private Handler handlerMain; // 待机的主界面的handle

    public Handler getHandler() {
        return handlerMain;
    }

    public void setHandler(Handler handler) {
        this.handlerMain = handler;
    }

    /**
     * Access to current global Context of the application, pay attention to use
     * this method must be in the AndroidManifest. XML configuration
     * MyApplication
     *
     * @return Context
     * @Title: getAppContext
     */
    public static Context getAppContext() {
        if (null == myApplication) {
            throw new RuntimeException(
                    "Please AndroidManifest. XML configuration MyApplication");
        }
        return myApplication;
    }

    /**
     * Access to current global Application of the Application, pay attention to
     * use this method must be in the AndroidManifest. XML configuration
     * MyApplication
     *
     * @return Application
     * @Title: getAppContext
     */
    public static MyApplication getApp() {
        if (null == myApplication) {
            throw new RuntimeException(
                    "Please AndroidManifest. XML configuration MyApplication");
        }
        return myApplication;
    }






    ///////////////////////////////////////////////////////////////////////////////

    /**
     * 判断是否开启了自动亮度调节
     *
     * @param aContentResolver
     * @return
     */
    public static boolean isAutoBrightness(ContentResolver aContentResolver) {
        boolean automicBrightness = false;
        try {
            automicBrightness = Settings.System.getInt(aContentResolver,
                    Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
        } catch (SettingNotFoundException e) {
            e.printStackTrace();
        }
        return automicBrightness;
    }

    /**
     * 获取屏幕的亮度
     *
     * @param activity
     * @return
     */
    public static int getScreenBrightness(Activity activity) {
        int nowBrightnessValue = 0;
        ContentResolver resolver = activity.getContentResolver();
        try {
            nowBrightnessValue = android.provider.Settings.System.getInt(
                    resolver, Settings.System.SCREEN_BRIGHTNESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nowBrightnessValue;
    }

    /**
     * 设置亮度
     *
     * @param activity
     * @param brightness
     */
    public static void setBrightness(Activity activity, int brightness) {
        // Settings.System.putInt(activity.getContentResolver(),
        // Settings.System.SCREEN_BRIGHTNESS_MODE,
        // Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
        activity.getWindow().setAttributes(lp);

    }

    /**
     * 停止自动亮度调节
     *
     * @param activity
     */
    public static void stopAutoBrightness(Activity activity) {
        Settings.System.putInt(activity.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }

    /**
     * 开启亮度自动调节
     *
     * @param activity
     */
    public static void startAutoBrightness(Activity activity) {
        Settings.System.putInt(activity.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
    }


    /**
     * 保存亮度设置状态
     *
     * @param resolver
     * @param brightness
     */
    public static void saveBrightness(final ContentResolver resolver, final int brightness) {
        singleThreadExecutor.execute(new Runnable() {
            @Override
            public void run() {
                Uri uri = android.provider.Settings.System
                        .getUriFor("screen_brightness");
                android.provider.Settings.System.putInt(resolver, "screen_brightness",
                        brightness);
                // resolver.registerContentObserver(uri, true, myContentObserver);
                resolver.notifyChange(uri, null);
            }
        });

    }

    public static MyApplication getInstance() {

        return application;

    }
    /**
     * Called when the application is starting, before any other application
     * objects have been created. Implementations should be as quick as possible
     * (for example using lazy initialization of state) since the time spent in
     * this function directly impacts the performance of starting the first
     * activity, service, or receiver in a process. If you override this method,
     * be sure to call super.onCreate().
     */

    public  SharePreferenceUtil preferences;
    private static MyApplication application ;
    private Boolean fig;
    private Boolean Code;
    Map<String,String> map=new HashMap<String, String>();
    public   String name;
    public   String Card;
    public   String number;
    public  long  seconds;
    public   String CardSQ;
      int CardSerial ;
    @Override
    public void onCreate() {
        application = this;
        myApplication = this;
        super.onCreate();
        x.Ext.init(this);

        if(LeakCanary.isInAnalyzerProcess(this)){
            return;
        }
//        refWatcher = LeakCanary.install(this);

        preferences = new SharePreferenceUtil(this,"smart");
        Glide.get(getApplicationContext()).setMemoryCategory(MemoryCategory.HIGH);

        Log.d(LOGTAG, "\n\n\n\n\n:" + b);// start...

        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(this);
        strategy.setCrashHandleCallback(new CrashReport.CrashHandleCallback() {
            @Override
            public synchronized Map<String, String> onCrashHandleStart(int crashType, String errorType, String errorMessage, String errorStack) {
                Handler handler = getHandler();
                if (handler != null)
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            restartApp();
                        }
                    });
                return super.onCrashHandleStart(crashType, errorType, errorMessage, errorStack);
            }
        });

        CrashReport.initCrashReport(this, "900012929", false);
    }
     /*   public static RefWatcher getRefWatcher(Context context) {
        MyApplication  application= (MyApplication) context.getApplicationContext();
        return application.refWatcher;
    }*/
    public void restartApp() {
        Intent intent = new Intent(this, Splash.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public String getNumber() {
        return number;
    }
    public void setCard(String Card) {
        this.Card = Card;
    }
    public String getCard() {
        return Card;
    }
    public void setCardSQ(String CardSQ) {
        this.CardSQ = CardSQ;
    }
    public String getCardSQ() {
        return CardSQ;
    }
    public void setSeconds(long seconds) {
        this.seconds = seconds;
    }
    public long getSeconds() {
        return seconds;
    }

    public void setMap(Map<String,String> map)
    {
        this.map = map;
    }
    public Map<String,String> getMap()
    {
        return map;
    }
    public int getCardSerial() {
        return CardSerial;
    }

    public void setCardSerial(int cardSerial) {
        CardSerial = cardSerial;
    }
//    public void setCode(Boolean Code)
//    {
//        this.Code = Code;
//    }
//    public Boolean getCode()
//    {
//        return Code;
//    }
    public void setFig(boolean fig)
    {
        this.fig = fig;
    }
    public Boolean getFig()
    {
        return fig;
    }


}
